//
//  STModifyPasswordViewController.swift
//  SeaTransportManage
//
//  Created by guopengwen on 2021/11/30.
//

import UIKit
import Foundation

func createTextField(_ placeholder: String) -> UITextField {
    let textField = UITextField.init()
    textField.placeholder = placeholder
    textField.borderStyle = .roundedRect
    textField.layer.borderColor = UIColor.init(hexString: "#8F92A1", alpha: 1).cgColor
    return textField
}

class STModifyPasswordViewController: GPBaseViewController, UITextFieldDelegate {

    var orignTF: UITextField = createTextField("请输入至少6位字母数字组合")
    var newTF: UITextField = createTextField("请输入至少6位字母数字组合")
    var confirmTF: UITextField = createTextField("请输入至少6位字母数字组合")
    var orignPsd: String = ""
    var newPsd: String = ""
    var confirmPsd: String = ""
    let requestApi = GPRequestApi.init()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setNavBarTitle("修改密码")
        addTopBGView()
        
        let h = 300 * (kScreenWidth - 32) / 343
        let bgView = UIView.init()
        bgView.backgroundColor = UIColor.white
        bgView.layer.cornerRadius = 12
        view.addSubview(bgView)
        bgView.snp.makeConstraints { (make) in
            make.top.equalTo(14)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(h)
        }
   
        let orignLb = createLabel("原密码")
        bgView.addSubview(orignLb)
        orignLb.snp.makeConstraints { (make) in
            make.top.equalTo(20)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(22)
        }
        
        bgView.addSubview(orignTF)
        orignTF.snp.makeConstraints { (make) in
            make.top.equalTo(orignLb.snp.bottom).offset(10)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(40)
        }
        
        let newLb = createLabel("新密码")
        bgView.addSubview(newLb)
        newLb.snp.makeConstraints { (make) in
            make.top.equalTo(orignTF.snp.bottom).offset(20)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(22)
        }
        
        bgView.addSubview(newTF)
        newTF.snp.makeConstraints { (make) in
            make.top.equalTo(newLb.snp.bottom).offset(10)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(40)
        }
    
        let confirmLb = createLabel("确认密码")
        bgView.addSubview(confirmLb)
        confirmLb.snp.makeConstraints { (make) in
            make.top.equalTo(newTF.snp.bottom).offset(20)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(22)
        }
        
        bgView.addSubview(confirmTF)
        confirmTF.snp.makeConstraints { (make) in
            make.top.equalTo(confirmLb.snp.bottom).offset(10)
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.height.equalTo(40)
        }
        
        view.addSubview(changeBtn)
        changeBtn.snp.makeConstraints { (make) in
            make.top.equalTo(bgView.snp.bottom).offset(60)
            make.left.equalTo(28)
            make.right.equalTo(-28)
            make.height.equalTo(58)
        }
        
        orignTF.delegate = self
        newTF.delegate = self
        confirmTF.delegate = self
        changeBtn.addTarget(self, action: #selector(changePSDAction), for: .touchUpInside)
    }
    
    @objc func changePSDAction() {
        self.view.endEditing(true)
        if(orignPsd.count < 6) {
            toastInfo("原密码至少6位字母数字组合")
        } else if(newPsd.count < 6) {
            toastInfo("新密码至少6位字母数字组合")
        } else if(confirmPsd.count < 6) {
            toastInfo("请再次输入至少6位字母数字组合的新密码")
        } else if(!newPsd.equalTo(confirmPsd)) {
            toastInfo("两次输入的新密码不一致")
        } else {
            let oldPassword = NSString.md5EncryptStr(orignPsd, bateNum: 32, isLowercaseStr: true)
            let newPassword = NSString.md5EncryptStr(newPsd, bateNum: 32, isLowercaseStr: true)
            let param = [ "oldPassword": oldPassword, "password": newPassword ]
            requestApi.updatePassword(param as [String : Any]) {
                (isSuccess, response, error)  in
                if(isSuccess) {
                    self.toastInfo("密码修改成功")
                    UserDefaults.standard.removeObject(forKey: Authorization_Key)
                    let loginVC: STLoginViewController = STLoginViewController.init()
                    UIApplication.shared.keyWindow?.rootViewController = loginVC
                } else {
                    self.toastError(error?.1 ?? "")
                }
            }
        }
    }
    
    // UITextFieldDelegate
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if textField.text != nil {
            let str = (textField.text! as NSString).replacingCharacters(in: range, with: string)
            if(textField == orignTF) {
                orignPsd = str
            } else if(textField == newTF) {
                newPsd = str
            } else {
                confirmPsd = str
            }
        }
        return true
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    
    
    func createLabel(_ str: String) -> UILabel {
        let lb = UILabel.init()
        lb.textAlignment = .left
        lb.text = str
        lb.textColor = UIColor.init(hexString: "#323232", alpha: 1)
        lb.font = UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.heavy)
        return lb
    }
    
    lazy var changeBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.setTitle("修改密码", for: .normal)
        btn.backgroundColor = UIColor.init(hexString: "#3686FF", alpha: 1)
        btn.layer.cornerRadius = 12
        btn.setTitleColor(UIColor.white, for: UIControl.State.normal)
        return btn
    }()

}
